<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyword" placeholder="请输入关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getEmployees">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

    <!--列表-->
		<el-table :data="rows" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
              @row-dblclick="dataBackFillGoods(row,column,event)"
              style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
      <el-table-column prop="username" label="员工姓名" width="120" sortable>
      </el-table-column>
      <el-table-column prop="age" label="员工年龄" width="120" sortable>
      </el-table-column>
      <el-table-column prop="state" label="状态" width="120" sortable>
        <template scope="scope">
          <span v-if="scope.row.state==0" style="color: green">上班</span>
          <span v-else-if="scope.row.state==-1" style="color: red">休假</span>
          <span v-else style="color: #8c939d">离职</span>
        </template>
      </el-table-column>
			<el-table-column prop="email" label="邮箱" width="180" sortable>
			</el-table-column>
			<el-table-column prop="phone" label="电话" width="180" sortable>
			</el-table-column>
			<el-table-column prop="password" label="密码" width="240" sortable>
			</el-table-column>
	<!--		<el-table-column prop="department_id" label="所属部门id" width="180" sortable>
			</el-table-column>-->
			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
    </el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next"
                     @current-change="handleCurrentChange"
                     :pageSize="pageSize"
                     :total="total"
                     :currentPage="currentPage"
                     style="float:right;">
			</el-pagination>
		</el-col>




    <!--编辑界面-->
		<el-dialog title="编辑" :visible.sync="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">

				<el-form-item label="姓名" prop="username">
					<el-input v-model="editForm.username" auto-complete="off"></el-input>
        </el-form-item>

				<el-form-item label="状态" prop="state">
					  <el-radio-group v-model="editForm.state">
						  <el-radio class="radio" :label="0">上班</el-radio>
						<el-radio class="radio" :label="1">离职</el-radio>
					</el-radio-group>
				</el-form-item>

				<el-form-item label="年龄" prop="age">
					<el-input v-model="editForm.age" :min="0" :max="200"></el-input>
				</el-form-item>

           <el-form-item label="电话" prop="phone">
          <el-input v-model="editForm.phone" auto-complete="off"></el-input>
           </el-form-item>

          <el-form-item label="邮件" prop="email">
          <el-input v-model="editForm.email" auto-complete="off"></el-input>
			  	</el-form-item>

				<el-form-item label="密码" prop="password">
					<el-input v-model="editForm.password" auto-complete="off"></el-input>
				</el-form-item>
      </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click.native="editFormVisible = false">取消</el-button>
          <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
        </div>
		</el-dialog>



    <!--抽屉框-->
    <el-drawer
        title="看我套娃"
        :visible.sync="emptable"
        direction="rtl"
        size="50%">
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
          <el-form :inline="true" :model="filters">
            <el-form-item>
              <el-input v-model="filters.keyword" placeholder="请输入关键字"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" v-on:click="getEmployees">查询</el-button>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="handleAdd">新增</el-button>
            </el-form-item>
          </el-form>
        </el-col>

        <el-table :data="rows" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
                  style="width: 100%;">
          <el-table-column type="selection" width="55">
          </el-table-column>
          <el-table-column type="index" width="60">
          </el-table-column>
          <el-table-column prop="username" label="员工姓名" width="120" sortable>
          </el-table-column>
          <el-table-column prop="age" label="员工年龄" width="120" sortable>
          </el-table-column>
          <el-table-column prop="state" label="状态" width="120" sortable>
            <template scope="scope">
              <span v-if="scope.row.state==0" style="color: green">上班</span>
              <span v-else-if="scope.row.state==-1" style="color: red">休假</span>
              <span v-else style="color: #8c939d">离职</span>
            </template>
          </el-table-column>
          <el-table-column prop="email" label="邮箱" width="180" sortable>
          </el-table-column>
          <el-table-column prop="phone" label="电话" width="180" sortable>
          </el-table-column>
          <el-table-column prop="password" label="密码" width="240" sortable>
          </el-table-column>
          <!--		<el-table-column prop="department_id" label="所属部门id" width="180" sortable>
              </el-table-column>-->
          <el-table-column label="操作" width="150">
            <template scope="scope">
              <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
              <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <el-col :span="24" class="toolbar">
        <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
        <el-pagination layout="prev, pager, next"
                       @current-change="handleCurrentChange"
                       :pageSize="pageSize"
                       :total="total"
                       :currentPage="currentPage"
                       style="float:right;">
        </el-pagination>
      </el-col>

    </el-drawer>


	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	import { getEmployeeListPage, removeEmployee, batchRemoveEmployee, editEmployee, addEmployee } from '../../api/api';

	export default {
		data() {
			return {
				filters: {
          keyword: ''
				},
				rows: [],
				total: 0,
        currentPage: 1,
        pageSize:5,
				listLoading: false,
				sels: [],//列表选中列

        emptable:false,
				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				editFormRules: {
					name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					]
				},
				//编辑界面数据
				editForm: {
					id: null,
          username:'',
          email:'',
          phone:'',
          password:'',
          age:null,
          state:0
        }

		/*		addFormVisible: false,//新增界面是否显示
				addLoading: false,
				addFormRules: {
					name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					]
				},
				//新增界面数据
				addForm: {
					name: '',
					sex: -1,
					age: 0,
					birth: '',
					addr: ''
				}
*/
			}
		},
		methods: {
      dataBackFillGoods :function (row, column,event){
        console.log(33);
        this.emptable=true;
      },
			//性别显示转换
		/*	formatSex: function (row, column) {
				return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
			},*/
      //点击页码触发事件
			handleCurrentChange(val) {
				this.currentPage = val;
				this.getEmployees();
			},
			//获取用户列表  也是高级查询的方法
			getEmployees() {
				let para = {
          currentPage: this.currentPage,
          keyword: this.filters.keyword
				};
				this.listLoading = true;
        this.$http.post("/emp",para)
          .then(result => {
            this.listLoading = false;
            result=result.data;
            this.total=result.total;
            this.rows=result.data;
          })
          .catch(result=>{
            this.$message({
              message: '查询失败',
              type: 'error'
            });
          })


				//NProgress.start();
			/*	getEmployeeListPage(para).then((res) => {
					this.total = res.data.total;
					this.employees = res.data.employees;
					this.listLoading = false;
					//NProgress.done();
				});*/
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '温馨提示', {
					type: 'warning'
				}).then(() => {//确定
					this.listLoading = true;
					//NProgress.start();
					/*let para = { id: row.id };*/
					this.$http.delete("/emp/"+row.id)
            .then(result=>{
              this.currentPage=1;
              this.getEmployees();
              //不用关闭忙等框 getEmployees 调用这个方法的时候里面有关闭忙等框
            })
            .catch(result=>{
              this.currentPage=1;
              this.getEmployees();
            })
					/*removeEmployee(para).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getEmployees();
					});*/
				}).catch(() => {
          //取消
				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				this.editFormVisible = true;
				this.editForm = Object.assign({}, row);
			},
			//显示新增界面
			handleAdd: function () {
        this.editFormVisible = true;
				this.editForm = {
          id: null,
          username:'',
          email:'',
          phone:'',
          password:'',
          age:null,
          state:0
				};
			},
			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
						  //开启忙等按钮
							this.editLoading = true;
							//NProgress.start();
							let para = Object.assign({}, this.editForm);
							this.$http.put("/emp",para)
                .then(result=>{
                  this.editFormVisible = false;
                  result=result.data;
                  if(result.success){
                    this.$message({
                      message: '操作成功',
                      type: 'success'
                    });
                    this.currentPage=1;
                    this.getEmployees();
                  }else{
                    this.$message({
                      message: '程序出错，正在殴打程序员',
                      type: 'error'
                    });
                  }
                  this.editLoading = false;
                })
                .catch(result=>{
                  this.editLoading = false;
                  this.$message({
                    message: '网络繁忙，请稍后再试',
                    type: 'error'
                  });
                })
						});
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id).toString();
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let para = { ids: ids };
					console.log(para);
          this.$http.patch("/emp",para)
            .then(result=>{
              this.currentPage=1;
              this.getEmployees();
            })
            .catch(result=>{
              this.currentPage=1;
              this.getEmployees();
            })
				}).catch(() => {

				});
			}
		},
		mounted() {
			this.getEmployees();
		}
	}

</script>

<style scoped>

</style>